package com.tanhua.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.groups.Default;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersControler {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserService userService;

    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if (userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {

        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 1. 用户资料--更新头像
     *
     * @param headPhoto
     * @return
     */
    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto) throws IOException {

        userInfoService.changeHeader(headPhoto);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号--发送短信验证码
     *
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {

        userService.sendVerificationCode();
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号--校验验证码
     *
     * @param map
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {

        String verificationCode = (String) map.get("verificationCode");
        Boolean result = userService.checkVerificationCode(verificationCode);
        Map<String, Boolean> remap = new HashedMap();
        remap.put("verification", result);
        return ResponseEntity.ok(remap);
    }

    /**
     * 保存手机号
     *
     * @param map
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity savePhone(@RequestBody Map map) {

        String phone = (String) map.get("phone");
        userService.savePhone(phone);
        return ResponseEntity.ok(null);
    }

    /*
     *互相喜欢，喜欢，粉丝 - 统计
     * */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        //我喜欢谁数据统计
        Integer counts = userService.counts();

        //粉丝数量统计
        Integer fancounts = userService.fanCounts();

        //互相喜欢数据统计
        Integer eachloveCount = userService.eachLoveCount();
        //封装数据
        Map map = new HashMap();
        map.put("loveCount", counts);
        map.put("eachLoveCount", eachloveCount);
        map.put("fanCount", fancounts);
        //响应结果
        return ResponseEntity.ok(map);
    }


    //互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
    @GetMapping("/friends/{type}")
    public ResponseEntity friends(@PathVariable("type") Integer type,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pagesize, String nickname) {


        PageResult pageResult = userService.findLove(type, nickname, page, pagesize);

        return ResponseEntity.ok(pageResult);
    }

    //喜欢 - 取消
    @DeleteMapping("like/{uid}")
    public ResponseEntity isLike(@PathVariable("uid") Long uid) {
        userService.update(uid);


        return ResponseEntity.ok(null);
    }

    //粉丝喜欢
    @PostMapping("/fans/{uid}")
    public ResponseEntity fanslike(@PathVariable("uid") Long uid) {
        userService.like(uid);

        return ResponseEntity.ok(null);
    }

}
